<template>
  <el-card class="box-card">
    <el-form
      ref="ruleFormRef"
      :model="ruleForm"
      :rules="rules"
      label-width="100px"
      class="demo-ruleForm"
      :size="formSize"
      status-icon
    >
      <el-form-item label="商品分类" prop="count">
        <el-cascader
          v-model="ruleForm.count"
          :options="options"
          placeholder="Activity count"
          style="width: 300px; height: 40px"
        />
      </el-form-item>

      <el-form-item label="商品名称" prop="name">
        <el-input v-model="ruleForm.name" style="width: 300px; height: 40px" />
      </el-form-item>
      <el-form-item label="商品简介" prop="desc">
        <el-input
          v-model="ruleForm.desc"
          type="textarea"
          rows="1"
          style="width: 300px"
        />
      </el-form-item>
      <el-form-item label="商品价格" prop="name">
        <el-input
          v-model="ruleForm.name"
          style="width: 300px; height: 40px"
          type="number"
        />
      </el-form-item>
      <el-form-item label="商品售卖价" prop="name">
        <el-input
          v-model="ruleForm.name"
          style="width: 300px; height: 40px"
          type="number"
        />
      </el-form-item>
      <el-form-item label="商品库存" prop="name">
        <el-input
          v-model="ruleForm.name"
          style="width: 300px; height: 40px"
          type="number"
        />
      </el-form-item>
      <el-form-item label="商品标签" prop="name">
        <el-input v-model="ruleForm.name" style="width: 300px; height: 40px" />
      </el-form-item>

      <el-form-item label="上架状态" prop="resource">
        <el-radio-group v-model="ruleForm.resource">
          <el-radio label="上架" />
          <el-radio label="下架" />
        </el-radio-group>
      </el-form-item>
      <el-form-item label="详情内容" prop="resource">
        <div style="border: 1px solid #ccc">
          <Toolbar
            style="border-bottom: 1px solid #ccc"
            :editor="editorRef"
            :defaultConfig="toolbarConfig"
            :mode="mode"
          />
          <Editor
            style="height: 500px; overflow-y: hidden"
            v-model="valueHtml"
            :defaultConfig="editorConfig"
            :mode="mode"
            @onCreated="handleCreated"
          />
        </div>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm(ruleFormRef)"
          >Create</el-button
        >
        <el-button @click="resetForm(ruleFormRef)">Reset</el-button>
      </el-form-item>
    </el-form>
  </el-card>
</template>

<script setup lang="ts">
import { reactive, onBeforeUnmount, ref, shallowRef, onMounted } from "vue";
import type { FormInstance, FormRules } from "element-plus";
import "@wangeditor/editor/dist/css/style.css"; // 引入 css

import { Editor, Toolbar } from "@wangeditor/editor-for-vue";

const formSize = ref("default");
const ruleFormRef = ref<FormInstance>();
const ruleForm = reactive({
  name: "Hello",
  region: "",
  count: "",
  date1: "",
  date2: "",
  delivery: false,
  type: [],
  resource: "",
  desc: "",
});
const mode = "default";
const rules = reactive<FormRules>({
  name: [
    { required: true, message: "Please input Activity name", trigger: "blur" },
    { min: 3, max: 5, message: "Length should be 3 to 5", trigger: "blur" },
  ],
  region: [
    {
      required: true,
      message: "Please select Activity zone",
      trigger: "change",
    },
  ],
  count: [
    {
      required: true,
      message: "Please select Activity count",
      trigger: "change",
    },
  ],
  date1: [
    {
      type: "date",
      required: true,
      message: "Please pick a date",
      trigger: "change",
    },
  ],
  date2: [
    {
      type: "date",
      required: true,
      message: "Please pick a time",
      trigger: "change",
    },
  ],
  type: [
    {
      type: "array",
      required: true,
      message: "Please select at least one activity type",
      trigger: "change",
    },
  ],
  resource: [
    {
      required: true,
      message: "Please select activity resource",
      trigger: "change",
    },
  ],
  desc: [
    { required: true, message: "Please input activity form", trigger: "blur" },
  ],
});

const submitForm = async (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  await formEl.validate((valid, fields) => {
    if (valid) {
      console.log("submit!");
    } else {
      console.log("error submit!", fields);
    }
  });
};

const resetForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  formEl.resetFields();
};

const options = Array.from({ length: 10000 }).map((_, idx) => ({
  value: `${idx + 1}`,
  label: `${idx + 1}`,
}));

//富文本框
const editorRef = shallowRef();

// 内容 HTML
const valueHtml = ref("<p>hello</p>");

// 模拟 ajax 异步获取内容
onMounted(() => {
  setTimeout(() => {
    valueHtml.value = "<p>模拟 Ajax 异步设置内容</p>";
  }, 1500);
});

const toolbarConfig = {};
const editorConfig = { placeholder: "请输入内容..." };

// 组件销毁时，也及时销毁编辑器
onBeforeUnmount(() => {
  const editor = editorRef.value;
  if (editor == null) return;
  editor.destroy();
});

const handleCreated = (editor) => {
  editorRef.value = editor; // 记录 editor 实例，重要！
};
</script>

<style>
</style>